using AiQiuQuan.Sport.Model.Models;
using AiQiuQuan.Sport.Core;
using AiQiuQuan.Sport.WebApi.Services;
using Microsoft.AspNetCore.Mvc;
using System.Text;
using Microsoft.AspNetCore.Authorization;

namespace AiQiuQuan.Sport.WebApi.Controllers
{
    /// <summary>
    /// 支付单管理
    /// </summary>
    public class PayOrderController : ApiController
    {
        private readonly IPayOrderService _payOrderService;
        private readonly IPayService _payService;

        /// <summary>
        /// 支付单管理
        /// </summary>
        public PayOrderController(
            IPayOrderService payOrderService,
            IPayService payService)
        {
            _payOrderService = payOrderService;
            _payService = payService;
        }

        /// <summary>
        /// 支付成功查询
        /// </summary>
        [HttpGet("get_payorder")]
        public async Task<UnaryResult<PaySuccessDto>> GetPayOrderInfoAsync(Guid orderId)
        {
            var result = await _payOrderService.GetPayOrderInfoAsync(orderId);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 查询支付信息
        /// </summary>
        [HttpPost("query_pay_info")]
        public async Task<UnaryResult<PayInfoDto>> GetPayInfoAsync(PayInfoRequestDto request)
        {
            var result = await _payService.GetPayInfoAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 支付
        /// </summary>
        [HttpPost("pay")]
        public async Task<UnaryResult<PayResultDto>> PayAsync(PayRequestDto request)
        {
            var result = await _payService.PayAsync(request);
            return result;
        }

        /// <summary>
        /// 退款
        /// </summary>
        [HttpPost("refund")]
        [AllowAnonymous]
        public async Task<UnaryResult<RefundResultDto>> RefundAsync(RefundRequestDto request)
        {
            var result = await _payService.RefundAsync(request);
            return result;
        }

        /// <summary>
        /// 支付单分页查询
        /// </summary>
        [HttpPost("pagelist")]
        [AllowAnonymous]
        public async Task<UnaryResult<PageResult<PayOrderDto>>> PageListAsync(PayOrderPageRequestDto request)
        {
            var result = await _payOrderService.PageListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 微信支付回调
        /// </summary>
        [HttpPost("wxpay_callback")]
        [AllowAnonymous]
        [ApiExplorerSettings(IgnoreApi = true)]
        [DisabeUnaryResult]
        public async Task<WxResponseDto> WxPayCallback(
            [FromHeader(Name = "Wechatpay-Timestamp")] string timestamp,
            [FromHeader(Name = "Wechatpay-Nonce")] string nonce,
            [FromHeader(Name = "Wechatpay-Signature")] string signature,
            [FromHeader(Name = "Wechatpay-Serial")] string serialNumber)
        {
            var request = new WxPayCallbackDto
            {
                Timestamp = timestamp,
                Nonce = nonce,
                Signature = signature,
                SerialNumber = serialNumber
            };

            using var reader = new StreamReader(Request.Body, Encoding.UTF8);
            request.Content = await reader.ReadToEndAsync();

            var result = await _payService.PayCallbackAsync(request);
            return result;
        }
    }
}
